package com.zhuoshi.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhuoshi.bean.Wd_apartment_layoutModel;
import com.zhuoshi.bean.Wd_ht_buildModel;
import com.zhuoshi.bean.Wd_ht_projectModel;
import com.zhuoshi.bean.Wd_ht_roomModel;
import com.zhuoshi.bean.Wd_ht_unitModel;
import com.zhuoshi.bean.Wd_purposeModel;
import com.zhuoshi.service.Wd_apartment_layoutModelService;
import com.zhuoshi.service.Wd_ht_buildModelService;
import com.zhuoshi.service.Wd_ht_projectModelService;
import com.zhuoshi.service.Wd_ht_roomModelService;
import com.zhuoshi.service.Wd_ht_unitModelService;
import com.zhuoshi.service.Wd_purposeModelService;
import com.zhuoshi.serviceImpl.Wd_purposeModelServiceImpl;

@Controller
@RequestMapping("/room")
public class Wd_ht_roomAction {
	@Autowired
	Wd_ht_roomModelService wd_ht_roomModelService ;
	@Autowired
	Wd_ht_projectModelService wd_ht_projectModelService;
	@Autowired
	Wd_ht_buildModelService wd_ht_buildModelService ;
	@Autowired
	Wd_ht_unitModelService wd_ht_unitModelService ;
	@Autowired
	Wd_apartment_layoutModelService wd_apartment_layoutModelService ;
    @Autowired
	Wd_purposeModelService wd_purposeModelService ;
	
    @RequestMapping("/selectAll.do")
	public String selectAll(Wd_ht_roomModel record,Model model){
    	 List<Wd_ht_roomModel> Wd_ht_roomList = wd_ht_roomModelService.selectAll(record);
		 model.addAttribute("Wd_ht_roomList", Wd_ht_roomList);
    	 return "Wd_ht_room" ;
	}
     
     @RequestMapping("/Wd_ht_room_toUpdate.do")
 	public String wd_ht_room_toUpdate(String roomId,Model model){
 		Wd_ht_roomModel bean = wd_ht_roomModelService.selectByPrimaryKey(roomId);
 		model.addAttribute("bean", bean);
 		List<Wd_apartment_layoutModel> Wd_apartment_layoutList = wd_apartment_layoutModelService.selectAll(null);
 		List<Wd_purposeModel> Wd_purposeList = wd_purposeModelService.selectAll(null);
 		model.addAttribute("Wd_apartment_layoutList", Wd_apartment_layoutList);
 		model.addAttribute("Wd_purposeList",Wd_purposeList );
 		return "Wd_ht_room_update" ;
 	}
 	
 	
 	
 	@RequestMapping("/Wd_ht_room_update.do")
 	public String wd_ht_room_update(Wd_ht_roomModel record,Model model){	
 		wd_ht_roomModelService.updateByPrimaryKeySelective(record) ;
 		return "redirect:/room/selectAll.do" ;
 	}
 	
 	@RequestMapping("/Wd_ht_room_toAdd.do")
 	public String wd_ht_room_toAdd(Model model){
 		List<Wd_ht_projectModel> Wd_ht_projectList = wd_ht_projectModelService.selectAll(null);
 		model.addAttribute("Wd_ht_projectList", Wd_ht_projectList);
 		List<Wd_apartment_layoutModel> Wd_apartment_layoutList = wd_apartment_layoutModelService.selectAll(null);
 		model.addAttribute("Wd_apartment_layoutList", Wd_apartment_layoutList);
 		List<Wd_purposeModel> Wd_purposeAll = wd_purposeModelService.selectAll(null);
 		model.addAttribute("Wd_purposeAll", Wd_purposeAll);
 		return "Wd_ht_room_add" ;
 	}
 	
 	
 	@RequestMapping("/Wd_ht_room_toManyAdd.do")
 	public String wd_ht_room_toManyAdd(){
 		
 		return "createRoom";
 	}
 	
 	@RequestMapping("/Wd_ht_room_add.do")
 	public String wd_ht_room_add(Wd_ht_roomModel record){	
 		wd_ht_roomModelService.insertSelective(record);
 		return "redirect:/room/selectAll.do" ;
 	}
 	
 	@RequestMapping("/Wd_ht_room_delete.do")
 	public String wd_ht_room_delete(String roomId){
 		wd_ht_roomModelService.deleteByPrimaryKey(roomId);
 		return "redirect:/room/selectAll.do" ;
 	}
 	
 	//ajax获取楼栋
 	@RequestMapping("/getBuild.do")
 	public @ResponseBody Map<String,Object> getBuild(String projectId){
 		List<Wd_ht_buildModel> Wd_ht_buildList = wd_ht_buildModelService.selectByProjectId(projectId);
 		Map<String,Object> jsonMap = new HashMap<String, Object>();
 		List list = null;
 		if(null!=Wd_ht_buildList && Wd_ht_buildList.size()>0){
 			list = new ArrayList();
 			for(Wd_ht_buildModel bean:Wd_ht_buildList){
 				Map<String,String> taskMap=new HashMap<String,String>();
 				taskMap.put("buildId", bean.getBuildId());
 				taskMap.put("buildName", bean.getBuildName());
 				list.add(taskMap);
 			}
 		}
 		jsonMap.put("tasks", list);
 	    return jsonMap;
 		
 	}
 	
 	//ajax获取单元
 	@RequestMapping("/getUnit.do")
 	public @ResponseBody Map<String,Object> getUnit(String buildId){
 		List<Wd_ht_unitModel> Wd_ht_unitList = wd_ht_unitModelService.selectByBuildId(buildId);
 		Map<String,Object> jsonMap = new HashMap<String, Object>();
 		List list = null;
 		if(null!=Wd_ht_unitList && Wd_ht_unitList.size()>0){
 			list = new ArrayList();
 			for(Wd_ht_unitModel bean:Wd_ht_unitList){
 				Map<String,String> taskMap=new HashMap<String,String>();
 				taskMap.put("unitId", bean.getUnitId());
 				taskMap.put("unitCode", bean.getUnitCode());
 				list.add(taskMap);
 			}
 		}
 		jsonMap.put("tasks", list);
 	    return jsonMap;
 		
 	}
 	
 
 	
 	@InitBinder
 	public void InitBinder(ServletRequestDataBinder binder){
 		binder.registerCustomEditor(Date.class, 
 				new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
 		
 	}
}
